import config from './config.js'
const baseUrl = config.host


const request = {
	get(url, data, contentType) {
		let headerObj = {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
		if ('json' == contentType) {
			headerObj = {
				'Content-Type': 'application/json'
			};
		}
		return new Promise((resolve, reject) => {
			uni.request({
				header: headerObj,
				url: baseUrl + url, //仅为示例，并非真实接口地址。
				method: 'GET',
				data: data,
			}).then(response => {
				let [err, res] = response
				resolve(res)
			}).catch(error => {
				let [err, res] = error
				reject(err)
			})
		});
	},

	post(url, data, contentType) {
		let headerObj = {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
		if ('json' == contentType) {
			headerObj = {
				'Content-Type': 'application/json'
			};
		}
		return new Promise((resolve, reject) => {
			uni.request({
				header: headerObj,
				url: baseUrl + url, //仅为示例，并非真实接口地址。
				method: 'POST',
				data: data,
			}).then(response => {
				let [err, res] = response
				resolve(res)
			}).catch(error => {
				let [err, res] = error
				reject(err)
			})
		});
	},
}

export default request;
